package com.beiming.libra.controller;

import com.beiming.libra.common.Assert;
import com.beiming.libra.common.constants.ValidateMessage;
import com.beiming.libra.common.enums.ErrorCode;
import com.beiming.libra.common.page.PageResult;
import com.beiming.libra.common.utils.FileUtil;
import com.beiming.libra.domain.ArbitrationCaseLog;
import com.beiming.libra.domain.CaseStatus;
import com.beiming.libra.domain.User;
import com.beiming.libra.domain.dto.requestdto.ArbitrationBusinessDisputesUpdateRequestDTO;
import com.beiming.libra.domain.dto.requestdto.ArbitrationCaseActionUpdateRequestDTO;
import com.beiming.libra.domain.dto.requestdto.ArbitrationCaseRequestDTO;
import com.beiming.libra.domain.dto.requestdto.ArbitrationCaseUpdateRequestDTO;
import com.beiming.libra.domain.dto.requestdto.CommonIdListRequestDTO;
import com.beiming.libra.domain.dto.requestdto.CommonIdPageRequestDTO;
import com.beiming.libra.domain.dto.requestdto.CommonIdRequestDTO;
import com.beiming.libra.domain.dto.responsedto.ArbitrationCaseBasicResponseDTO;
import com.beiming.libra.domain.dto.responsedto.ArbitrationCaseResponseDTO;
import com.beiming.libra.domain.dto.responsedto.CaseUserEditQueryResponseDTO;
import com.beiming.libra.helper.ContextUtil;
import com.beiming.libra.helper.Result;
import com.beiming.libra.service.ArbitrationCaseLogService;
import com.beiming.libra.service.ArbitrationCaseService;
import com.beiming.libra.service.SignatureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 仲裁案件
 *
 * @author zhangqi
 */
@RestController
@Api(value = "案件模块", tags = "案件模块")
@RequestMapping(value = "api/arbitrationcase")
@Slf4j
public class ArbitrationCaseController {

  @Resource
  private ArbitrationCaseService arbitrationCaseService;
  @Resource
  private ArbitrationCaseLogService arbitrationCaseLogService;
  @Resource
  private SignatureService signatureService;
  /**
   * 案件列表
   */
  @ApiOperation(value = "案件基本信息列表", notes = "案件基本信息列表")
  @RequestMapping(value = "/CaseList", method = RequestMethod.POST)
  @ResponseBody
  public Result<PageResult<ArbitrationCaseBasicResponseDTO>> getCaseList(
      @RequestBody @Valid ArbitrationCaseRequestDTO arbitrationCaseReq) {
    return Result.success(arbitrationCaseService.getArbitrationCaseBasicList(arbitrationCaseReq,
        ContextUtil.getCurrentUserId()));
  }

  /**
   * 补正立案
   *
   */
  @ApiOperation(value = "案件(补正立案)基本信息查询", notes = "案件(补正立案)基本信息查询")
  @RequestMapping(value = "/Case", method = RequestMethod.POST)
  @ResponseBody
  public Result<ArbitrationCaseResponseDTO> getCase(@RequestBody @Valid CommonIdRequestDTO idDto) {
    return Result.success(arbitrationCaseService.getArbitrationCase(idDto.getId()));
  }

  /**
   * 补正立案更新案件基本信息
   *
   */
  @ApiOperation(value = "补正立案案件基本信息更新", notes = "补正立案案件基本信息更新")
  @RequestMapping(value = "/Case", method = RequestMethod.PUT)
  @ResponseBody
  public Result<Integer> updateCase(@RequestBody @Valid ArbitrationCaseUpdateRequestDTO acUpdateDTO) {
    return Result.success(arbitrationCaseService.updateCaseBasicInfo(acUpdateDTO));
  }

  /**
   * 补正立案更新业务要素
   *
   */
  @ApiOperation(value = "补正立案业务要素更新", notes = "补正立案业务要素更新")
  @RequestMapping(value = "/CaseBusinessDisputes", method = RequestMethod.PUT)
  @ResponseBody
  public Result<Integer> updateBusinessDisputes(@RequestBody @Valid ArbitrationBusinessDisputesUpdateRequestDTO abdUpdateDTO) {
    return Result.success(arbitrationCaseService.updateArbitrationBusinessDisputes(abdUpdateDTO));
  }

  /**
   * 办案秘书案件流程操作
   *
   */
  @ApiOperation(value = "办案秘书案件流程操作", notes = "办案秘书案件流程操作")
  @RequestMapping(value = "/CaseSecretary", method = RequestMethod.POST)
  @ResponseBody
  public Result caseSecretary(@RequestBody @Valid ArbitrationCaseActionUpdateRequestDTO acau) {
    return Result.success(arbitrationCaseService.updateCaseStatusForWork(acau));
  }

  /**
   * 仲裁员案件流程操作
   *
   */
  @ApiOperation(value = "仲裁员案件流程操作", notes = "仲裁员案件流程操作")
  @RequestMapping(value = "/CaseArbitrator", method = RequestMethod.POST)
  @ResponseBody
  public Result caseArbitrator(@RequestBody @Valid ArbitrationCaseActionUpdateRequestDTO acau) {
    return Result.success(arbitrationCaseService.updateCaseStatusForWork(acau));
  }

  /**
   * 领导案件流程操作
   *
   */
  @ApiOperation(value = "领导案件流程操作", notes = "领导案件流程操作")
  @RequestMapping(value = "/CaseLeader", method = RequestMethod.POST)
  @ResponseBody
  public Result caseLeader(@RequestBody @Valid ArbitrationCaseActionUpdateRequestDTO acau) {
    return Result.success(arbitrationCaseService.updateCaseStatusForWork(acau));
  }

  /**
   * 财务案件流程操作
   *
   */
  @ApiOperation(value = "财务案件流程操作", notes = "财务案件流程操作")
  @RequestMapping(value = "/CaseFinancial", method = RequestMethod.POST)
  @ResponseBody
  public Result caseFinancial(@RequestBody @Valid ArbitrationCaseActionUpdateRequestDTO acau) {
    return Result.success(arbitrationCaseService.updateCaseStatusForWork(acau));
  }

  /**
   * 秘书长案件流程操作
   *
   */
  @ApiOperation(value = "秘书长案件流程操作", notes = "秘书长案件流程操作")
  @RequestMapping(value = "/CaseSecretaryGeneral", method = RequestMethod.POST)
  @ResponseBody
  public Result caseSecretaryGeneral(@RequestBody @Valid ArbitrationCaseActionUpdateRequestDTO acau) {
    return Result.success(arbitrationCaseService.updateCaseStatusForWork(acau));
  }

  /**
   * 用户案件流程操作
   *
   */
  @ApiOperation(value = "用户案件流程操作", notes = "用户案件流程操作")
  @RequestMapping(value = "/caseUser", method = RequestMethod.POST)
  @ResponseBody
  public Result caseUser(@RequestBody @Valid ArbitrationCaseActionUpdateRequestDTO acau) {
    return Result.success(arbitrationCaseService.updateCaseStatusForUser(acau));
  }

  /**
   * 案件全进度查询
   *
   * @date 2018年4月28日
   */
  @ApiOperation(value = "案件全进度查询", notes = "案件全进度查询")
  @RequestMapping(value = "/CaseLog", method = RequestMethod.POST)
  @ResponseBody
  public Result<PageResult<ArbitrationCaseLog>> caseLog(
      @RequestBody @Valid CommonIdPageRequestDTO cir) {
    return Result.success(arbitrationCaseLogService.getArbitrationCaseLogList(cir));
  }

  /**
   * 获得案件进度类型
   */
  @RequestMapping(value = "caseStatus/{userType}", method = RequestMethod.POST)
  @ApiOperation(value = "案件进度类型", notes = "1为用户,2为工作人员")
  public Result<List<CaseStatus>> caseStatus(
      @ApiParam("用户/工作人员") @PathVariable("userType") Integer userType) {
    Assert.isNotNull(userType, ErrorCode.ILLEGAL_PARAMETER);
    return Result.success(arbitrationCaseService.selectCaseStatus(userType));
  }

  /**
   * 获得案件进度类型
   */
  @RequestMapping(value = "caseStatus", method = RequestMethod.POST)
  @ApiOperation(value = "案件所有进度类型", notes = "案件所有进度类型")
  public Result<List<CaseStatus>> caseStatusAll() {
    return Result.success(arbitrationCaseService.selectCaseStatus());
  }

  /**
   * 签署三书
   */
  @RequestMapping(value = "/confirmAsk", method = RequestMethod.POST)
  @ApiOperation(value = "签署三书", notes = "签署三书")
  public void confirmAsk(@ApiParam("三书类别") String id, @RequestParam("signFile") MultipartFile signFile)
      throws Exception {
    Assert.isNotNull(id, ErrorCode.ILLEGAL_PARAMETER);
    Assert.isNotNull(signFile, ErrorCode.EMPTY_FILE, ValidateMessage.NOT_FILE_UPLOAD);
    Assert.isTrue(FileUtil.isPicture(signFile), ErrorCode.FILE_TYPE_IS_WRONG,
        ValidateMessage.FILE_TYPE_IS_WRONG);
    //TODO 判断图片大小
    log.info("File size is {}", signFile.getSize());
    User user = ContextUtil.getLoginUserInfo();
    signatureService.confirmAsk(user, id, signFile);
  }

  /**
   * 当事人/代理人查询
   *
   */
  @ApiOperation(value = "案件详情当事人/代理人查询", notes = "案件详情当事人/代理人查询")
  @RequestMapping(value = "/CaseUserInfo/{id}", method = RequestMethod.GET)
  public Result<CaseUserEditQueryResponseDTO> getCaseUser(@ApiParam("案件ID") @PathVariable("id") String id) {
    return Result.success(arbitrationCaseService.selectCaseUser(id));
  }

  /**
   * 当事人/代理人同意
   *
   */
  @ApiOperation(value = "案件详情当事人/代理人审核同意", notes = "案件详情当事人/代理人审核同意")
  @RequestMapping(value = "/CaseUserInfo", method = RequestMethod.PUT)
  public Result updateCaseUser(@RequestBody @Valid CommonIdListRequestDTO idDto) {
    return Result.success(arbitrationCaseService.updateCaseUser(idDto.getId()));
  }

  /**
   * 当事人/代理人审核拒绝
   *
   */
  @ApiOperation(value = "案件详情当事人/代理人提交审核拒绝", notes = "案件详情当事人/代理人提交审核拒绝")
  @RequestMapping(value = "/CaseUserInfoRefuse", method = RequestMethod.PUT)
  public Result deleteCaseUser(@RequestBody @Valid CommonIdListRequestDTO idDto) {
    return Result.success(arbitrationCaseService.deleteCaseUser(idDto.getId()));
  }
}
